//
//  ProfileViewController.m
//  MyPizzaMobile
//
//  Created by Stanislav on 2/15/13.
//  Copyright (c) 2013 Stanislav. All rights reserved.
//

#import "ProfileViewController.h"

@interface ProfileViewController ()
@property (readwrite) RequestManager* requestManager;
@end

@implementation ProfileViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	self.requestManager = [RequestManager requestManagerInstance];
    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(saveProfileInfo)];
    self.navigationItem.rightBarButtonItem = anotherButton;    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setUsernameTextField:nil];
    [self setPasswordTextField:nil];
    [self setAddressTextField:nil];
    [self setPhoneTextField:nil];
    [self setOrdersAddressTextField:nil];
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    self.usernameTextField.text = [self.requestManager unescape:[defaults objectForKey:@"username"]];
    self.passwordTextField.text = [self.requestManager unescape:[defaults objectForKey:@"password"]];
    self.addressTextField.text = [self.requestManager unescape:[defaults objectForKey:@"address"]];
    self.phoneTextField.text = [self.requestManager unescape:[defaults objectForKey:@"phone"]];
    self.ordersAddressTextField.text = [self.requestManager unescape:[defaults objectForKey:@"ordersAddress"]];
}

- (IBAction)nextFieldPressed:(UITextField *)sender {
    if(sender == self.usernameTextField)
    {
        [self.passwordTextField becomeFirstResponder];
    }
    else if(sender == self.passwordTextField)
    {
        [self.addressTextField becomeFirstResponder];
    }
    else if (sender == self.addressTextField)
    {
        [self.phoneTextField becomeFirstResponder];
    }
    else if(sender == self.phoneTextField)
    {
        [self.ordersAddressTextField becomeFirstResponder];
    }
    else if(sender == self.ordersAddressTextField)
    {
        [self.ordersAddressTextField resignFirstResponder];
        [self saveProfileInfo];
    }
}

- (IBAction)dissmissKeyboard:(UITextField *)sender {
    [sender resignFirstResponder];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) {
        [self.usernameTextField resignFirstResponder];
        [self.passwordTextField resignFirstResponder];
        [self.addressTextField resignFirstResponder];
        [self.phoneTextField resignFirstResponder];
        [self.ordersAddressTextField resignFirstResponder];
        
    }
}


- (void) saveProfileInfo
{
    NSString* username = [self.requestManager escape:self.usernameTextField.text];
    NSString* password = [self.requestManager escape:self.passwordTextField.text];
    NSString* address = [self.requestManager escape:self.addressTextField.text];
    NSString* phone = [self.requestManager escape:self.phoneTextField.text];
    NSString* ordersAddress = [self.requestManager escape:self.ordersAddressTextField.text];
    NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
    [dict setObject:username forKey:@"username"];
    [dict setObject:password forKey:@"password"];
    [dict setObject:address forKey:@"address"];
    [dict setObject:phone forKey:@"phone"];
    [dict setObject:ordersAddress forKey:@"ordersAddress"];
    
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    id userId = [defaults objectForKey:@"id"];
    [dict setObject:userId forKey:@"id"];
    
    [self.requestManager updateUserFromDictionary:dict];
    [self.navigationController popViewControllerAnimated:YES];

}
@end
